enum HttpDataMethod
{
	POST,
	GET,
	BITMAP,
	SOUND
}
interface HttpDataCallback
{
	dealProgress(event:egret.ProgressEvent);
	dealSuccess(event:egret.Event);
	dealFail(event:egret.IOErrorEvent);
}
interface HttpDataLayerCallback
{
	layerSuccess();
	layerError();
}
class HttpData
{
	public m_Url:string;
	public m_HashCode:number;//post 1000   get 2000   bitmap  3000   sound 4000
	public m_Method:HttpDataMethod;
	public m_params:string;
	public m_Callback:HttpDataCallback;
	public static create(url:string, method:HttpDataMethod, params:string, hashCode:number ,callback:HttpDataCallback):HttpData
	{
		let httpData:HttpData = new HttpData();
		httpData.m_Url = url;
		httpData.m_Method = method;
		httpData.m_params = params;
		httpData.m_HashCode = hashCode;
		httpData.m_Callback = callback;
		return httpData;
	}
	public constructor() 
	{

	}
}
class HttpDataLayer
{
	private m_HttpDataQueue:Array<HttpData>;
	private m_ErrorHttpDataQueue:Array<HttpData>;
	private m_NowHttpData:HttpData;
	private m_HttpDataLayerCallback:HttpDataLayerCallback;
	public constructor()
	{
		this.m_HttpDataQueue = new Array();
		this.m_ErrorHttpDataQueue = new Array();
	}
	public cleanQueue()
	{
		this.m_NowHttpData = null;
		this.m_ErrorHttpDataQueue = null;
		this.m_HttpDataQueue = null;
	}
	public addToQueue(httpData:HttpData)
	{
		this.m_HttpDataQueue.push(httpData);
		if(this.m_NowHttpData == null)
		{
			this.doNext();
		}
	}
	public setCallback(callback:HttpDataLayerCallback)
	{
		this.m_HttpDataLayerCallback = callback;
	}
	public resetErrorAgain()
	{
		this.m_HttpDataQueue = this.m_ErrorHttpDataQueue;
		this.m_ErrorHttpDataQueue = new Array();
		this.doNext();
	}
	public postData()
	{
		console.log("postData");
		let hashCode = this.m_NowHttpData.m_HashCode;
		let m_HttpRequest = new egret.HttpRequest();
		m_HttpRequest.responseType = egret.HttpResponseType.TEXT;
		m_HttpRequest.$hashCode = hashCode;
		m_HttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
		m_HttpRequest.addEventListener(egret.Event.COMPLETE,this.dealSuccessCallback,this);
		m_HttpRequest.addEventListener(egret.IOErrorEvent.IO_ERROR,this.dealFailCallback,this);
		m_HttpRequest.addEventListener(egret.ProgressEvent.PROGRESS,this.dealProgressCallback,this)
		let url = this.m_NowHttpData.m_Url;
		let params = this.m_NowHttpData.m_params;
		m_HttpRequest.open(url, egret.HttpMethod.POST);
		m_HttpRequest.send(params);
	}
	public getData()
	{
		console.log("getData");
		let hashCode = this.m_NowHttpData.m_HashCode;
		let m_HttpRequest = new egret.HttpRequest();
		m_HttpRequest.responseType = egret.HttpResponseType.TEXT;
		m_HttpRequest.$hashCode = hashCode;
		m_HttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
		m_HttpRequest.addEventListener(egret.Event.COMPLETE,this.dealSuccessCallback,this);
		m_HttpRequest.addEventListener(egret.IOErrorEvent.IO_ERROR,this.dealFailCallback,this);
		m_HttpRequest.addEventListener(egret.ProgressEvent.PROGRESS,this.dealProgressCallback,this)
		let url = this.m_NowHttpData.m_Url;
		if((this.m_NowHttpData.m_params != null) && this.m_NowHttpData.m_params != "")
		{
			url = url + "?" + this.m_NowHttpData.m_params;
		}
		m_HttpRequest.open(url, egret.HttpMethod.GET);
		m_HttpRequest.send();
	}
	public bitMapData()
	{
		let hashCode = this.m_NowHttpData.m_HashCode;
		let url = this.m_NowHttpData.m_Url;
		var imgLoader:egret.ImageLoader = new egret.ImageLoader;
		imgLoader.$hashCode = hashCode;
		imgLoader.crossOrigin = 'anonymous';
		imgLoader.once(egret.Event.COMPLETE, this.dealSuccessCallback, this);
		imgLoader.once(egret.IOErrorEvent.IO_ERROR, this.dealFailCallback,this);
		imgLoader.once(egret.ProgressEvent.PROGRESS, this.dealProgressCallback,this)
		imgLoader.load(url);
	}
	public soundData()
	{
		let hashCode = this.m_NowHttpData.m_HashCode;
		let url = this.m_NowHttpData.m_Url;
		var soundLoader:egret.Sound = new egret.Sound;
		soundLoader.$hashCode = hashCode;
		soundLoader.once(egret.Event.COMPLETE, this.dealSuccessCallback, this);
		soundLoader.once(egret.IOErrorEvent.IO_ERROR,this.dealFailCallback,this);
		soundLoader.once(egret.ProgressEvent.PROGRESS,this.dealProgressCallback,this)
		soundLoader.load(url);
	}
	private dealProgressCallback(event:egret.ProgressEvent)
	{
		//console.log("hashCode:" + event.currentTarget.hashCode + " get progress : " + Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%");
		if(this.m_NowHttpData != null)
		{
			this.m_NowHttpData.m_Callback.dealProgress(event);
		}
	}
	private dealSuccessCallback(event:egret.Event)
	{
		//console.log("success hashCode:" + event.currentTarget.hashCode)
		if(this.m_NowHttpData != null)
		{
			this.m_NowHttpData.m_Callback.dealSuccess(event);
		}
		this.doNext();
	}
	private dealFailCallback(event:egret.IOErrorEvent)
	{
		console.log("error hashCode:" + this.m_NowHttpData.m_Url);
		this.m_ErrorHttpDataQueue.push(this.m_NowHttpData);
		if(this.m_NowHttpData != null)
		{
			this.m_NowHttpData.m_Callback.dealFail(event);
		}
		this.doNext();
	}
	public doNext()
	{
		if(this.m_HttpDataQueue.length > 0)
		{
			this.m_NowHttpData = this.m_HttpDataQueue.shift();
			//console.log("url:" + this.m_NowHttpData.m_Url);
			switch(this.m_NowHttpData.m_Method)
			{
				case HttpDataMethod.POST:
				{
					this.postData();
				}
				break;
				case HttpDataMethod.GET:
				{
					this.getData();
				}
				break;
				case HttpDataMethod.BITMAP:
				{
					this.bitMapData();
				}
				break;
				case HttpDataMethod.SOUND:
				{
					this.soundData();
				}
				break;
				default:
				{
					console.log("m_Method unknow type");
				}
				break;
			}
		}
		else
		{
			if(this.m_HttpDataLayerCallback != null)
			{
				if(this.m_ErrorHttpDataQueue.length == 0)
				{
					this.m_HttpDataLayerCallback.layerSuccess();
				}
				else
				{
					this.m_HttpDataLayerCallback.layerError();
				}
			}
		}
	}
}
class HttpConnector 
{
	private static s_HttpConnecotr:HttpConnector;
	public static getInstance():HttpConnector
	{
		if(this.s_HttpConnecotr == null)
		{
			this.s_HttpConnecotr = new HttpConnector();
		}
		return this.s_HttpConnecotr;
	}
	private m_HttpLayerDirectionary:Array<HttpDataLayer>;
	public constructor()
	{
		this.m_HttpLayerDirectionary = new Array();
	}
	public getLayer(key:string)
	{
		let httpDataLayer:HttpDataLayer = this.m_HttpLayerDirectionary[key];
		if(httpDataLayer == null)
		{
			httpDataLayer = new HttpDataLayer();
			this.m_HttpLayerDirectionary[key] = httpDataLayer;
		}
		return httpDataLayer;
	}
}